package me.zhengjie.modules.utils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author brains
 */
public class ResultToReportUtil {

    /**
     * 首要特质 A、T、M、R中得分最高的类型，如果一样高(得分都一样)，全部显示
     *
     * @param result 得分
     * @return 得分第二高
     */
    public static String calMaxResultType(String result) {
        List<Unit> newList = sortList(result);
        String maxResult = String.valueOf(newList.get(0).getType());
        if (newList.get(0).getScore() == newList.get(1).getScore()) {
            maxResult += "、" + newList.get(1).getType();
        }
        if (newList.get(0).getScore() == newList.get(2).getScore()) {
            maxResult += "、" + newList.get(2).getType();
        }
        if (newList.get(0).getScore() == newList.get(3).getScore()) {
            maxResult += "、" + newList.get(3).getType();
        }
        return maxResult;
    }

    /**
     * 次要特质 A、T、M、R中得分第二高的，如果没有第二高(得分都一样)，这条不显示
     *
     * @param result 得分
     * @return 得分第二高
     */
    public static String calCytz(String result) {
        List<Unit> newList = sortList(result);
        float num = secondNumber(result);
        if (num < 0) {
            return "";
        }
        return newList.stream().filter(s -> s.getScore() == num).map(Unit::getType).collect(Collectors.joining(","));
    }

    /**
     * 显性特质 定义总分为n，某项得分大于等于3n/5,则为显性特质，如果没有满足 不显示
     *
     * @param result       得分
     * @return 显性特质类型
     */
    public static String calXxtz(String result) {
        String[] results = result.split(",");
        int sum = 0;
        for (int i = 0; i <results.length ; i++) {
            sum = sum + Integer.parseInt(results[i]);
        }
        float avgNum = 0.6f;
        String xxtz = "";
        if (Float.parseFloat(results[0]) / sum >= avgNum) {
            xxtz += "A、";
        }
        if (Float.parseFloat(results[1]) / sum >= avgNum) {
            xxtz += "T、";
        }
        if (Float.parseFloat(results[2]) / sum >= avgNum) {
            xxtz += "M、";
        }
        if (Float.parseFloat(results[3]) / sum >= avgNum) {
            xxtz += "R、";
        }
        if (xxtz.length() > 0) {
            xxtz = xxtz.substring(0, xxtz.length() - 1);
        }
        return xxtz;
    }

    private static float secondNumber(String result) {
        float a1 = -1;
        float a2 = -1;
        String[] arr = result.split(",");
        for (String s : arr) {
            float tmp = Float.parseFloat(s);
            if (tmp > a1) {
                a2 = a1;
                a1 = tmp;
                continue;
            }
            if (tmp > a2 && tmp != a1) {
                a2 = tmp;
            }
        }
        return a2;
    }

    private static List<Unit> sortList(String result) {
        String[] resultStr = result.split(",");
        List<Unit> list = new ArrayList<>();
        Unit unitA = new Unit();
        unitA.setScore(Float.parseFloat(resultStr[0]));
        unitA.setType("A");
        Unit unitT = new Unit();
        unitT.setScore(Float.parseFloat(resultStr[1]));
        unitT.setType("T");
        Unit unitM = new Unit();
        unitM.setScore(Float.parseFloat(resultStr[2]));
        unitM.setType("M");
        Unit unitR = new Unit();
        unitR.setScore(Float.parseFloat(resultStr[3]));
        unitR.setType("R");
        list.add(unitA);
        list.add(unitT);
        list.add(unitM);
        list.add(unitR);
        return list.stream().sorted(Comparator.comparing(Unit::getScore).reversed())
                .collect(Collectors.toList());
    }

    public static Map<String, String> getReport(String result) {
        Map<String, String> model = new HashMap<>();
        List<Unit> unitList = new ArrayList<>();
        String[] results = result.split(",");
        String[] types = {"A", "T", "M", "R"};
        // 将分值与输出值都放入集合对象中
        for (int i = 0; i < results.length; i++) {
            Unit unit = new Unit();
            unit.setScore(Float.parseFloat(results[i]));
            unit.setType(types[i]);
            unitList.add(unit);
        }
        //根据分值排序 确定ATMR的顺序
        unitList.sort(Comparator.comparing(Unit::getScore).reversed());
        //输出字符
        StringBuilder sb = new StringBuilder();
        //临时存放重复值
        Set<Float> splitSet = new HashSet<>();
        //输出字符
        StringBuilder xxTezhi = new StringBuilder();
        for (int i = 0; i < unitList.size(); i++) {
            splitSet.add(unitList.get(i).getScore());
            sb.append(unitList.get(i).getType());
            // 自己和后一个比较 确定自己和后一个中间的符号
            if (i < unitList.size() - 1) {
                compare(unitList.get(i).getScore(), unitList.get(i + 1).getScore(), sb);
            }
            // 特殊逻辑 超过75分输出
            if (unitList.get(i).getScore() >= 75) {
                xxTezhi.append(",").append(unitList.get(i).getType());
            }
        }
        String resultPage = sb.toString().toLowerCase();
        String reportTezhi = sb.toString().replaceAll("_", ",");
        // 特殊逻辑 当集合中多个值相同时 确定ATMR全部相等 添加尾号
        if (splitSet.size() == 1) {
            float a = unitList.get(0).getScore();
            if (a < 50) {
                resultPage += "_1";
            } else if (a >= 50 && a <= 75) {
                resultPage += "_2";
            } else {
                resultPage += "_3";
            }
        }
        if (StringUtils.isNotEmpty(xxTezhi)) {
            model.put("xxTezhi", xxTezhi.substring(1));
        }
        model.put("resultPage", resultPage);
        model.put("reportTezhi", reportTezhi);
        return model;
    }

    /**
     * float比较确定输出符号
     *
     * @param a  比较值
     * @param b  比较值
     * @param sb 输出字符
     */
    private static void compare(Float a, Float b, StringBuilder sb) {
        if (a.equals(b)) {
            sb.append("-");
        } else {
            sb.append("_");
        }
    }
}

